package com.hs.egova.service.impl;

import com.hs.egova.base.Pagination;
import com.hs.egova.base.ResultInfo;
import com.hs.egova.constant.ResultKeyConst;
import com.hs.egova.dao.UserDao;
import com.hs.egova.mapping.User;
import com.hs.egova.service.UserManager;
import com.hs.egova.tool.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author husheng
 * @version v1.0
 * @date 2021/7/26 16:36
 * @email husheng@egova.com.cn
 */
@Service
@Slf4j
public class UserManagerImpl implements UserManager {
    @Autowired
    private UserDao userDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ResultInfo getUserById(Long id) {
        ResultInfo resultInfo = new ResultInfo(true);
        User user = userDao.findById(id);
        resultInfo.setData("data", user);
        log.info("***user**** {}", user.toString());
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByName(String userName) {
        ResultInfo resultInfo = new ResultInfo(true);
        String whereSql = "userName like concat('%', ?0, '%')";
        Object[] params = new Object[]{userName};
        //String whereSql = "userName = ?0";
        List<User> users = userDao.queryByWhere(whereSql, params);
        resultInfo.setData("list", users);
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByCustom(String userName) {
        ResultInfo resultInfo = new ResultInfo(true);
        User user = new User();
        user.setUserName(userName);
        user.setNote("自定义测试userName");
        user.setId(1L);
        resultInfo.setData(ResultKeyConst.DATA, user);
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByUserNameAndNote(String userName, String note) {
        ResultInfo resultInfo = new ResultInfo(true);
        String whereSql = "userName = ?0 and note = ?1";
        Object[] params = new Object[]{userName, note};
        List<User> users = userDao.queryByWhere(whereSql, params);
        resultInfo.setData("list", users);
        return resultInfo;
    }

    @Override
    public ResultInfo page(int currentPage, int numPerPage) {
        ResultInfo resultInfo = new ResultInfo(true);
        String sql = "from user";
        Pagination<User> userPaginations = userDao.getPagination(sql, currentPage, numPerPage);
        resultInfo.setData("list", userPaginations.getResultList());
        resultInfo.setData("pageInfo", userPaginations.getPageInfo());
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByUserName(String userName) {
        ResultInfo resultInfo = new ResultInfo(true);
        String strHql = "user_name = :userName";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userName", userName);
        List<User> users = userDao.queryByWhere(strHql, paramMap);
        resultInfo.setData(ResultKeyConst.LIST, users);
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByUserNameNew(String userName) {
        ResultInfo resultInfo = new ResultInfo(true);
        List<User> users = userDao.queryByWhere("userName = ?0", userName);
        resultInfo.setData(ResultKeyConst.LIST, users);
        return resultInfo;
    }

    @Override
    public ResultInfo getUserByIds(Integer[] ids) {
        ResultInfo resultInfo = new ResultInfo(true);
        String idsString = StringUtils.joinString(ids,",");
        log.info("idsString: {}", idsString);
        String sql = "id in (" + idsString + ")";
        String newSql = "id in ('" + idsString.replace(",", "','") + "') ";
        List<User> users = userDao.queryByWhere(newSql);
        resultInfo.setData(ResultKeyConst.LIST, users);
        return resultInfo;
    }


}
